<?php
namespace Admin\Form;
use Zend\Form\Form;
use Zend\Form\Element;
use Zend\InputFilter;
use Zend\Debug\Debug;
class AddProductForm extends Form{
	public function __construct($name = null){
		parent::__construct('AddProduct');
		$this->setAttribute('method','post');
		$this->setAttribute('enctype','multipart/form-data');

		$this->add(array(
           'name'=>'maHang',
           'attributes'=>array(
                'type'=>'text',
                'required'=>'required',
                'id'=>'maHang',
                'class'=>'form-control',
                               ),
           'options'=>array(
				'label'=>'Mã sản phẩm',
				'label_attributes'=>array(
				    'class'=>'col-sm-3 control-label',
					),
				),
           'filters'=>array(
                array('name'=>'StringTrim'),
				),
        ));
        $this->add(array(
		    'name'=>'ten',
		    'attributes'=>array(
				'type'=>'text',
				'required'=>'required',
				'id'=>'ten',
				'class'=>'form-control',
			),
			'options'=>array(
				'label'=>'Tên',
				'label_attributes'=>array(
				    'class'=>'col-sm-3 control-label',
					),
			),
			'filters'=>array(
				'name'=>'StringTrim',
			),
		));
		$this->add(array(
		    'name'=>'soLuong',
		    'attributes'=>array(
				'type'=>'text',
				'required'=>'required',
				'id'=>'soLuong',
				'class'=>'form-control',
				'onfocus' => 'clickModal();'  /* hàm được định nghĩa ở view */
			),
			'options'=>array(
				'label'=>'Số lượng',
				'label_attributes'=>array(
				    'class'=>'col-sm-3 control-label',
					),
			),
			'filters'=>array(
				'name'=>'StringTrim',
			),
		));

		$this->add(array(
		    'name'=>'tinhTrang',
		    'attributes'=>array(
				'type'=>'text',
				'id'=>'tinhTrang',
				'class'=>'form-control',
				'readonly' => 'readonly'
			),
			'options'=>array(
				'label'=>'Tinh Trạng',
				'label_attributes'=>array(
				    'class'=>'col-sm-3 control-label',
					),
			),
			'filters'=>array(
				'name'=>'StringTrim',
			),
		));

		$this->add(array(
		    'name'=>'moTa',
		    'attributes'=>array(
				'type'=>'textarea',
				'required'=>'required',
				'id'=>'moTa',
				'class'=>'form-control',

			),
			'options'=>array(
				'label'=>'Mô tả',
				'label_attributes'=>array(
				    'class'=>'col-sm-3 control-label',
					),
			),
			'filters'=>array(
				'name'=>'StringTrim',
			),
		));

		$this->add(array(
		    'name'=>'ngayNhap',
		    'attributes'=>array(
				'type'=>'date',
				'required'=>'required',
				'id'=>'ngayNhap',
				'class'=>'form-control',
			),
			'options'=>array(
				'label'=>'Ngày nhập',
				'label_attributes'=>array(
				    'class'=>'col-sm-3 control-label',
					),
			),
			'filters'=>array(
				'name'=>'StringTrim',
			),
		));

		$this->add(array(
		    'name'=>'gia',
		    'attributes'=>array(
				'type'=>'text',
				'required'=>'required',
				'id'=>'gia',
				'class'=>'form-control',
			),
			'options'=>array(
				'label'=>'Đơn giá',
				'label_attributes'=>array(
				    'class'=>'col-sm-3 control-label',
					),
			),
			'filters'=>array(
				'name'=>'StringTrim',
			),
		));

		$this->add(array(
		    'name'=>'hinhAnh1',
		    'attributes'=>array(
				'type'=>'file',
				'required'=>'required',
				//'id'=>'hinhAnh1',
				//'class'=>'form-control',
			),
			'options'=>array(
				'label'=>'Ảnh 1:',
				'label_attributes'=>array(
				    'class'=>'col-sm-3 control-label',
					),
			),
			'filters'=>array(
				'name'=>'StringTrim',
			),
		));

		$this->add(array(
		    'name'=>'hinhAnh2',
		    'attributes'=>array(
				'type'=>'file',
				//'required'=>'required',
				//'id'=>'hinhAnh2',
				//'class'=>'form-control',
			),
			'options'=>array(
				'label'=>'Ảnh 2:',
				'label_attributes'=>array(
				    'class'=>'col-sm-3 control-label',
					),
			),
			'filters'=>array(
				'name'=>'StringTrim',
			),
		));

		$this->add(array(
		    'name'=>'hinhAnh3',
		    'attributes'=>array(
				'type'=>'file',
				//'required'=>'required',
				//'id'=>'hinhAnh3',
				//'class'=>'form-control',
			),
			'options'=>array(
				'label'=>'Ảnh 3:',
				'label_attributes'=>array(
				    'class'=>'col-sm-3 control-label',
					),
			),
			'filters'=>array(
				'name'=>'StringTrim',
			),
		));

		$this->add(array(
		           'type'=>'select',
		           'name'=>'loaiHang',
		           'attributes'=>array(
		            	'id'=>'loaiHang',
		            	'class'=>'form-control',
					),
		           'options'=>array(
		                	'label'=>'Loại hàng: ',
		                	'label_attributes'=>array(
				    'class'=>'col-sm-3 control-label',
					),
		                	'value_options'=>array(
		                		'1'=>'Giày dép',
		                		'2'=>'Túi xách',
		                	),
		                ),
		           ));

		$this->add(array(
		    'name'=>'submit',
		    'attributes'=>array(
				'type'=>'submit',
				'value'=>'Thêm',
				'id'=>'submit',
				'class'=>'form-control',
				'class'=>'btn btn-primary',
			),
		));
	}

	// hàm upload file ảnh
	public function upload($files = array(), $filePaths = ''){
		$fileName = array();
		$fileName[0] = 0;
		if(count($files)!=0 && $filePaths!=''){
			$uploadObj = new \Zend\File\Transfer\Adapter\Http();
			$uploadObj->setDestination($filePaths);
			$counter = 0;
			foreach ($files as $key => $value) {
				if($uploadObj->receive($key) && $value['name']!=''){
				  	$counter++;
					$fileName[$counter] = $value['name'];
				}
			}
		}
		// kiểm tra thành công hay chưa
		if($counter>0)
			$fileName[0] = 1;
		return $fileName;
	}
}